package com.qlk.vorder.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.vorder.domain.OrderNote;
import com.qlk.vorder.sdk.api.request.OrderNoteRequest;
import com.qlk.vorder.sdk.api.response.OrderNoteResponse;
import com.qlk.vorder.service.OrderNoteService;

/**
 * 便签REST服务：提供有关便签的接口
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:01
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public class OrderNoteResource {

	private final Log logger = LogFactory.getLog(this.getClass());

	@Autowired
	private OrderNoteService orderNoteService;

	/**
	 * 查询便签信息
	 * 
	 * @param request
	 *            便签请求参数
	 * @return 便签返回对象
	 * 
	 */
	@POST
	@Path("/orderNote/getOrderNote")
	public Wrapper<?> getOrderNote(OrderNoteRequest request) {
		if (null == request || null == request.getId()) {
			this.logger.error("getOrderNote 传入参数有误");
			return WrapMapper.illegalArgument();
		}

		try {
			OrderNote orderNote = orderNoteService.getOrderNoteById(request.getId());
			OrderNoteResponse response = convert(orderNote);
			return WrapMapper.ok().result(response);
		} catch (Exception e) {
			this.logger.error("查询便签数据异常", e);
			return WrapMapper.error();
		}
	}

	// 数据转换
	private OrderNoteResponse convert(OrderNote orderNote) {
		if (null == orderNote) {
			return null;
		}

		OrderNoteResponse orderNoteResponse = new OrderNoteResponse();
		BeanUtils.copyProperties(orderNote, orderNoteResponse);
		return orderNoteResponse;
	}

	// 数据转换
	private List<OrderNoteResponse> convertList(List<OrderNote> orderNotes) {
		if (CollectionUtils.isEmpty(orderNotes)) {
			return null;
		}

		List<OrderNoteResponse> list = new ArrayList<OrderNoteResponse>(orderNotes.size());
		for (OrderNote orderNote : orderNotes) {
			list.add(convert(orderNote));
		}
		return list;
	}

}
